
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 6 (SUF) ****************************
************************* THIS FILE RUNS THE LIKELIHOOD ************************
********************************************************************************
********************************************************************************

*Version: 2023 March 29


cap mkdir `"$output/model_estimation"'
cap mkdir `"$output/model_estimation/relax_SUF"'
cap mkdir `"$output/log/relax_SUF_6_model_estimation"'
cap mkdir `"$output/model_estimation/relax_SUF/parameters"'

cap log close 
log using `"$output/log/relax_SUF_6_model_estimation/relax_SUF_6_model_estimation.smcl"', replace 

di "This run uses code version from 2023/03/29"



****************************************************************
****************************************************************
******************* Model Estimation by Size *******************
****************************************************************
****************************************************************
use  `"$output/temp/relax_SUF_estimation_data_bin.dta"', clear

//set criteria for observations to be included in the liklihood
bys pay_type_pooled state visit_code_cat: gen cell_obs = _N 
gen no_resub_line = 0
replace no_resub_line = 1 if choice_S == 1 & n_S == . 
bys visit_billing_id: gegen no_resub = max(no_resub_line)
bys pay_type_pooled state visit_code_cat: gegen cell_sd =  sd(no_resub)
replace cell_sd = 0 if cell_sd == . 
gen valid_cell = 0 
replace valid_cell = 1 if cell_obs >= 10 & cell_sd > 0 


gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



** constraint on coefficient of continuation value **
constraint 1 continuation = 1

gen line_cost = .
gen line_cost_se = .
gen claim_cost = .
gen claim_cost_se = .

gen line_cost_adm = . 
gen line_cost_adm_se = . 
gen line_cost_cont = . 
gen line_cost_cont_se = . 
gen line_cost_cov = . 
gen line_cost_cov_se = . 
gen line_cost_dup = . 
gen line_cost_dup_se = .  
gen line_cost_info = . 
gen line_cost_info_se = . 

gen claim_cost_adm = . 
gen claim_cost_adm_se = . 
gen claim_cost_cont = . 
gen claim_cost_cont_se = . 
gen claim_cost_cov = . 
gen claim_cost_cov_se = . 
gen claim_cost_dup = . 
gen claim_cost_dup_se = . 
gen claim_cost_info = . 
gen claim_cost_info_se = . 

label var line_cost "Line Parameter"
label var line_cost_se "Line Parameter SE"
label var claim_cost "Claim Parameter "
label var claim_cost_se "Claim Parameter SE"

label var line_cost_adm "Line Parameter Administrative"
label var line_cost_adm_se "Line Parameter Administrative SE"
label var claim_cost_adm "Claim Parameter Administrative"
label var claim_cost_adm_se "Claim Parameter Administrative SE"

label var line_cost_cont "Line Parameter Contractual Adjustment"
label var line_cost_cont_se "Line Parameter Contractual Adjustment SE"
label var claim_cost_cont "Claim Parameter Contractual Adjustment"
label var claim_cost_cont_se "Claim Parameter Contractual Adjustment SE"

label var line_cost_cov "Line Parameter Coverage"
label var line_cost_cov_se "Line Parameter Coverage SE"
label var claim_cost_cov "Claim Parameter Coverage"
label var claim_cost_cov_se "Claim Parameter Coverage SE"

label var line_cost_dup "Line Parameter Duplicate"
label var line_cost_dup_se "Line Parameter Duplicate SE"
label var claim_cost_dup "Claim Parameter Duplicate"
label var claim_cost_dup_se "Claim Parameter Duplicate SE"

label var line_cost_info "Line Parameter Information"
label var line_cost_info_se "Line Parameter Information SE"
label var claim_cost_info "Claim Parameter Information"
label var claim_cost_info_se "Claim Parameter Information SE"


cap xi, noomit  : gen i.state

forv s = 1/51 {
	
	cap replace _Istate_`s' = 0 if n_S == .

}
	
gen extra_line = (-n_S+1)
replace extra_line = 0 if n_S == .


forvalues size = 1/2 {
	
	if `size' == 1 {
		local grup_size "small"
	}
	
	if `size' == 2 {
		local grup_size "large"
	}
	

	forvalues x = 1/3 {
		
		if `x' == 1 {
			local payer "Medicare"
		}
		
		else if `x' == 2 {
			local payer "Medicaid"
		}
		
		else if `x' == 3 {
			local payer "Commercial"
		}
		
		forvalues r = 1/5 {
		
			if `r' == 1 {
				local reason "administrative"
			}
			
			else if `r' == 2 {
				local reason "contractual"
			}
			
			else if `r' == 3 {
				local reason "coverage"
			}
		
			else if `r' == 4 {
				local reason "duplicate"
			}
			
			
			else if `r' == 5 {
				local reason "information"
			}
		
		
			** MLE - heterogeneity by payer and reason code and reason code-specific constant resubmission cost 
			di `"These are the estimates for `payer' for reason code category `reason' and group size `grup_size'"'
			clogit choice_S _Istate* extra_line continuation if pay_type_pooled ==`x' & visit_code_cat_n == `r' & size_indicator == `size' & valid_cell == 1, constraint(1) group(visit_billing_id) iter($iterations)
			
			est save `"$output/model_estimation/relax_SUF/parameters/model_est_`payer'_`reason'_`grup_size'groups.ster"', replace 


			cap replace line_cost     =  _b[extra_line]   if pay_type_pooled == `x'    &    visit_code_cat_n == `r' & size_indicator == `size'
			cap replace line_cost_se  =  _se[extra_line]  if pay_type_pooled == `x' &    visit_code_cat_n == `r' & size_indicator == `size'

			
			forv s = 1/54 {
				cap replace claim_cost	= _b[_Istate_`s'] if state == `s'    & pay_type_pooled == `x' &    visit_code_cat_n == `r' & size_indicator == `size'
				cap replace claim_cost_se = _se[_Istate_`s'] if state == `s'  & pay_type_pooled == `x' &    visit_code_cat_n == `r' & size_indicator == `size'
			}


		
		
			if (`r' == 1) {
			
				cap replace line_cost_adm = _b[extra_line] if pay_type_pooled == `x' & size_indicator == `size'
				cap replace line_cost_adm_se = _se[extra_line] if pay_type_pooled == `x' & size_indicator == `size'

				forv s = 1/54 {
				
					cap replace claim_cost_adm = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_adm_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 2) {
			
				cap replace line_cost_cont = _b[extra_line] if pay_type_pooled == `x' & size_indicator == `size'
				cap replace line_cost_cont_se = _se[extra_line] if pay_type_pooled == `x' & size_indicator == `size'

				forv s = 1/54 {
				
					cap replace claim_cost_cont = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_cont_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					
				}
			}
			
			else if (`r' == 3) {
			
				cap replace line_cost_cov = _b[extra_line] if pay_type_pooled == `x' & size_indicator == `size'
				cap replace line_cost_cov_se = _se[extra_line] if pay_type_pooled == `x' & size_indicator == `size'

				forv s = 1/54 {
				
					cap replace claim_cost_cov = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_cov_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 4) {
			
				cap replace line_cost_dup = _b[extra_line] if pay_type_pooled == `x' & size_indicator == `size'
				cap replace line_cost_dup_se = _se[extra_line] if pay_type_pooled == `x' & size_indicator == `size'

				forv s = 1/54 {
				
					cap replace claim_cost_dup = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_dup_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 5) {
			
				cap replace line_cost_info = _b[extra_line] if pay_type_pooled == `x' & size_indicator == `size'
				cap replace line_cost_info_se = _se[extra_line] if pay_type_pooled == `x' & size_indicator == `size'
				
				forv s = 1/54 {
				
					cap replace claim_cost_info = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_info_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
		}		
	
	}

}

* keep relevant observations
bys pay_type_pooled state visit_code_cat size_indicator: keep if _n == 1
keep pay_type_pooled state line_cost* claim_cost* payee_state_cd visit_code_cat visit_code_cat_n size_indicator
compress

save "$output/model_estimation/relax_SUF/estimations_bin.dta", replace 

keep if pay_type_pooled == 2 
bys pay_type_pooled state size_indicator: keep if _n == 1
cap drop line_cost line_cost_se claim_cost claim_cost_se visit_code_cat visit_code_cat_n size_indicator

save "$output/model_estimation/relax_SUF/estimations_medicaid_bin.dta", replace 







****************************************************************
****************************************************************
********** Model Estimation No Size Distinction ****************
****************************************************************
****************************************************************
use  `"$output/temp/relax_SUF_estimation_data_bin.dta"', clear

//set criteria for observations to be included in the liklihood
bys pay_type_pooled state visit_code_cat: gen cell_obs = _N 
gen no_resub_line = 0
replace no_resub_line = 1 if choice_S == 1 & n_S == . 
bys visit_billing_id: gegen no_resub = max(no_resub_line)
bys pay_type_pooled state visit_code_cat: gegen cell_sd =  sd(no_resub)
replace cell_sd = 0 if cell_sd == . 
gen valid_cell = 0 
replace valid_cell = 1 if cell_obs >= 10 & cell_sd > 0 


gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



** constraint on coefficient of continuation value **
constraint 1 continuation = 1

gen line_cost = .
gen line_cost_se = .
gen claim_cost = .
gen claim_cost_se = .

gen line_cost_adm = . 
gen line_cost_adm_se = . 
gen line_cost_cont = . 
gen line_cost_cont_se = . 
gen line_cost_cov = . 
gen line_cost_cov_se = . 
gen line_cost_dup = . 
gen line_cost_dup_se = .  
gen line_cost_info = . 
gen line_cost_info_se = . 

gen claim_cost_adm = . 
gen claim_cost_adm_se = . 
gen claim_cost_cont = . 
gen claim_cost_cont_se = . 
gen claim_cost_cov = . 
gen claim_cost_cov_se = . 
gen claim_cost_dup = . 
gen claim_cost_dup_se = . 
gen claim_cost_info = . 
gen claim_cost_info_se = . 

label var line_cost "Line Parameter"
label var line_cost_se "Line Parameter SE"
label var claim_cost "Claim Parameter "
label var claim_cost_se "Claim Parameter SE"

label var line_cost_adm "Line Parameter Administrative"
label var line_cost_adm_se "Line Parameter Administrative SE"
label var claim_cost_adm "Claim Parameter Administrative"
label var claim_cost_adm_se "Claim Parameter Administrative SE"

label var line_cost_cont "Line Parameter Contractual Adjustment"
label var line_cost_cont_se "Line Parameter Contractual Adjustment SE"
label var claim_cost_cont "Claim Parameter Contractual Adjustment"
label var claim_cost_cont_se "Claim Parameter Contractual Adjustment SE"

label var line_cost_cov "Line Parameter Coverage"
label var line_cost_cov_se "Line Parameter Coverage SE"
label var claim_cost_cov "Claim Parameter Coverage"
label var claim_cost_cov_se "Claim Parameter Coverage SE"

label var line_cost_dup "Line Parameter Duplicate"
label var line_cost_dup_se "Line Parameter Duplicate SE"
label var claim_cost_dup "Claim Parameter Duplicate"
label var claim_cost_dup_se "Claim Parameter Duplicate SE"

label var line_cost_info "Line Parameter Information"
label var line_cost_info_se "Line Parameter Information SE"
label var claim_cost_info "Claim Parameter Information"
label var claim_cost_info_se "Claim Parameter Information SE"


cap xi, noomit  : gen i.state

forv s = 1/51 {
	
	cap replace _Istate_`s' = 0 if n_S == .

}
	
gen extra_line = (-n_S+1)
replace extra_line = 0 if n_S == .


forvalues x = 1/3 {
	
	if `x' == 1 {
		local payer "Medicare"
	}
	
	else if `x' == 2 {
		local payer "Medicaid"
	}
	
	else if `x' == 3 {
		local payer "Commercial"
	}
	
	forvalues r = 1/5 {
	
		if `r' == 1 {
			local reason "Administrative"
		}
		
		else if `r' == 2 {
			local reason "contractual"
		}
		
		else if `r' == 3 {
			local reason "coverage"
		}
	
		else if `r' == 4 {
			local reason "duplicate"
		}
		
		
		else if `r' == 5 {
			local reason "information"
		}
	
	
		** MLE - heterogeneity by payer and reason code and reason code-specific constant resubmission cost 
		di `"These are the estimates for `payer' for reason code category `reason' and group size `grup_size'"'
		clogit choice_S _Istate* extra_line continuation if pay_type_pooled ==`x' & visit_code_cat_n == `r' & valid_cell == 1, constraint(1) group(visit_billing_id) iter($iterations)
		
		est save `"$output/model_estimation/relax_SUF/parameters/model_est_`payer'_`reason'_allgroups.ster"', replace 


		cap replace line_cost     =  _b[extra_line]   if pay_type_pooled == `x'    &    visit_code_cat_n == `r'
		cap replace line_cost_se  =  _se[extra_line]  if pay_type_pooled == `x' &    visit_code_cat_n == `r'

		
		forv s = 1/54 {
			cap replace claim_cost	= _b[_Istate_`s'] if state == `s'    & pay_type_pooled == `x' &    visit_code_cat_n == `r'
			cap replace claim_cost_se = _se[_Istate_`s'] if state == `s'  & pay_type_pooled == `x' &    visit_code_cat_n == `r'
		}


	
	
		if (`r' == 1) {
		
			cap replace line_cost_adm = _b[extra_line] if pay_type_pooled == `x'
			cap replace line_cost_adm_se = _se[extra_line] if pay_type_pooled == `x'

			forv s = 1/54 {
			
				cap replace claim_cost_adm = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_adm_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
			
			}
		}
		
		else if (`r' == 2) {
		
			cap replace line_cost_cont = _b[extra_line] if pay_type_pooled == `x'
			cap replace line_cost_cont_se = _se[extra_line] if pay_type_pooled == `x'

			forv s = 1/54 {
			
				cap replace claim_cost_cont = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_cont_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				
			}
		}
		
		else if (`r' == 3) {
		
			cap replace line_cost_cov = _b[extra_line] if pay_type_pooled == `x'
			cap replace line_cost_cov_se = _se[extra_line] if pay_type_pooled == `x'

			forv s = 1/54 {
			
				cap replace claim_cost_cov = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_cov_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
			
			}
		}
		
		else if (`r' == 4) {
		
			cap replace line_cost_dup = _b[extra_line] if pay_type_pooled == `x'
			cap replace line_cost_dup_se = _se[extra_line] if pay_type_pooled == `x'

			forv s = 1/54 {
			
				cap replace claim_cost_dup = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_dup_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
			
			}
		}
		
		else if (`r' == 5) {
		
			cap replace line_cost_info = _b[extra_line] if pay_type_pooled == `x'
			cap replace line_cost_info_se = _se[extra_line] if pay_type_pooled == `x'
			
			forv s = 1/54 {
			
				cap replace claim_cost_info = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_info_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
			
			}
		}
		
	}		

}



* keep relevant observations
bys pay_type_pooled state visit_code_cat: keep if _n == 1
keep pay_type_pooled state line_cost* claim_cost* payee_state_cd visit_code_cat visit_code_cat_n
compress

save "$output/model_estimation/relax_SUF/estimations_nosize_bin.dta", replace 

keep if pay_type_pooled == 2 
bys pay_type_pooled state: keep if _n == 1
cap drop line_cost line_cost_se claim_cost claim_cost_se visit_code_cat visit_code_cat_n

save "$output/model_estimation/relax_SUF/estimations_nosize_medicaid_bin.dta", replace 






****************************************************************
****************************************************************
******* Model Estimation No Size and Reason Distinction ********
****************************************************************
****************************************************************
use  `"$output/temp/relax_SUF_estimation_data_bin.dta"', clear

//set criteria for observations to be included in the liklihood
bys pay_type_pooled state visit_code_cat: gen cell_obs = _N 
gen no_resub_line = 0
replace no_resub_line = 1 if choice_S == 1 & n_S == . 
bys visit_billing_id: gegen no_resub = max(no_resub_line)
bys pay_type_pooled state visit_code_cat: gegen cell_sd =  sd(no_resub)
replace cell_sd = 0 if cell_sd == . 
gen valid_cell = 0 
replace valid_cell = 1 if cell_obs >= 10 & cell_sd > 0 

** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



** constraint on coefficient of continuation value **
constraint 1 continuation = 1

gen line_cost = .
gen line_cost_se = .
gen claim_cost = .
gen claim_cost_se = .


label var line_cost "Line Parameter"
label var line_cost_se "Line Parameter SE"
label var claim_cost "Claim Parameter "
label var claim_cost_se "Claim Parameter SE"

cap xi, noomit  : gen i.state

forv s = 1/51 {
	
	cap replace _Istate_`s' = 0 if n_S == .

}
	
gen extra_line = (-n_S+1)
replace extra_line = 0 if n_S == .


forvalues x = 1/3 {
	
	if `x' == 1 {
		local payer "Medicare"
	}
	
	else if `x' == 2 {
		local payer "Medicaid"
	}
	
	else if `x' == 3 {
		local payer "Commercial"
	}
	
	
	** MLE - heterogeneity by payer and reason code and reason code-specific constant resubmission cost 
	di `"These are the estimates for `payer' for reason code category `reason' and group size `grup_size'"'
	clogit choice_S _Istate* extra_line continuation if pay_type_pooled ==`x' & valid_cell == 1, constraint(1) group(visit_billing_id) iter($iterations)
	
	est save `"$output/model_estimation/relax_SUF/parameters/model_est_`payer'_allreasons_allgroups.ster"', replace 


	cap replace line_cost     =  _b[extra_line]   if pay_type_pooled == `x'
	cap replace line_cost_se  =  _se[extra_line]  if pay_type_pooled == `x'

	
	forv s = 1/54 {
		cap replace claim_cost	= _b[_Istate_`s'] if state == `s'    & pay_type_pooled == `x' 
		cap replace claim_cost_se = _se[_Istate_`s'] if state == `s'  & pay_type_pooled == `x'
	}	
	
}		



* keep relevant observations
bys pay_type_pooled state: keep if _n == 1
keep pay_type_pooled state line_cost* claim_cost* payee_state_cd
compress

save "$output/model_estimation/relax_SUF/estimations_nosize_noreason_bin.dta", replace 

keep if pay_type_pooled == 2 
bys pay_type_pooled state: keep if _n == 1
cap drop line_cost line_cost_se claim_cost claim_cost_se visit_code_cat visit_code_cat_n

save "$output/model_estimation/relax_SUF/estimations_nosize_noreason_medicaid_bin.dta", replace 


log close


